package nc.tb.ctl;

import java.util.HashMap;
import java.util.Iterator;

import nc.tb.bccsaction.Entry;
import nc.tb.pub.NtbEnv;
import nc.tb.pub.cache.NtbCacheManager;
import nc.tb.pub.data.INtbCache;

public class ConsolidateCTL implements INtbCache{
	private final static String CACHE_KEY = "ConsolidateCTL";
	private HashMap<String,Entry> hm_pk2entry;
	public void clearCache() {
		hm_pk2entry.clear();
		aConsolidateCTL = null;
	}
	private ConsolidateCTL() {
		hm_pk2entry = new HashMap<String,Entry>();
	}
	private static ConsolidateCTL aConsolidateCTL = null;
	public static ConsolidateCTL getInstance() {
		if (NtbEnv.isUICall) {
			if(aConsolidateCTL == null) {
				aConsolidateCTL = new ConsolidateCTL();
				NtbCacheManager.putCacheInstance(CACHE_KEY, aConsolidateCTL);
			}
			return aConsolidateCTL;
		}
		ConsolidateCTL inst = (ConsolidateCTL)NtbCacheManager.getCacheInstance(CACHE_KEY);
		if(inst == null) {
			inst = new ConsolidateCTL();
			NtbCacheManager.putCacheInstance(CACHE_KEY, inst);
		}
		return inst;
	}
	public void removePk(String pk) {
		hm_pk2entry.remove(pk);
	}
	public Entry getPk(String pk) {
		return hm_pk2entry.get(pk);
	}
	public void putValue(String pk, Entry et) {
		hm_pk2entry.put(pk, et);
	}
	public Entry[] getAllEntrys() {
		Entry[] result = hm_pk2entry == null ? new Entry[0] : new Entry[hm_pk2entry.size()];
		Iterator<Entry> it = hm_pk2entry.values().iterator();
		int idx = 0;
		while (it.hasNext()) {
			result[idx] = it.next();
			idx++;
		}
		return result;
	}
}
